package com.Sing.Controller;

import com.Sing.Pojo.Admin;
import com.Sing.Pojo.Notice;
import com.Sing.Service.NoticeService;
import com.Sing.Utils.Constant;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

@Controller
public class NoticeController {

    private NoticeService noticeService;
    @Autowired
    @Qualifier("noticeServiceImpl")
    public void setNoticeService(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    // 显示欢迎页面，刷新公告
    @RequestMapping("/welcome")
    public String welcomeUser( Model model ){

        ArrayList<Notice> allData = noticeService.getAllData();

        model.addAttribute( "noticeList", allData );

        return "welcome";
    }

    // 跳转至 ： 公告管理
    @RequestMapping("/noticeIndexOfBack")
    public String noticeIndexOfBack(){

        return "/notice/noticeIndexOfBack";
    }

    /**
     * 获取公告 提供给 公告管理
     * @param page 查询的起始页面
     * @param limit 查询的每一页的大小 pageSize
     * @param topic 查询条件
     */
    @RequestMapping("/noticeAll")
    @ResponseBody
    public String noticeAll( String page, String limit, String topic ) throws JsonProcessingException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageContext", ( Integer.parseInt( page ) - 1 ) * Integer.parseInt( limit ) );
        paramMap.put("pageSize", Integer.parseInt( limit ) );
        if ( topic != null ){
            topic = "%" + topic + "%";
        }
        paramMap.put("topic", topic);

        // 查询结果
        ArrayList<Notice> notices = noticeService.getDataByTopic(paramMap);

        // 用于返回结果的 Map
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put( "code", 0 );
        resultMap.put( "data", notices );
        resultMap.put( "count", noticeService.getDataByTopicCount( paramMap ) );

        return new ObjectMapper().writeValueAsString( resultMap );
    }

    // 删除表数据
    @RequestMapping("/deleteNoticeByIds")
    @ResponseBody
    public HashMap<String,Object> deleteNotice( String ids ) throws JsonProcessingException {
        HashMap<String, Object> resultMap = new HashMap<>();

        String[] split = ids.split(",");
        for ( String id : split ){
            // 将拆分出的数据转换为 Integer 型的数据，再进行删除
            int Id = Integer.parseInt(id);
            noticeService.deleteData( Id );
        }

        resultMap.put( "code", 0 );

        return resultMap;
    }

    // 查询表数据
    @RequestMapping("/queryNoticeById")
    public String queryNoticeById( String id, Model model ){

        Notice notice = noticeService.queryNoticeById(Integer.parseInt(id));

        model.addAttribute( "info", notice );

        return "notice/updateNotice";
    }

    // 跳转到 添加表数据
    @RequestMapping("/noticeAdd")
    public String noticeAddJump(){

        return "notice/noticeAdd";
    }

    /**
     * 添加新的公告
     * @param topic 传入 SQL 中的参数之一 ： 公告主题
     * @param content 传入 SQL 中的参数之一 ： 公告内容
     * @param req 用于获取 Session ，提供 Session 获取创建者的信息
     * @return 返回给 Ajax 请求
     */
    @RequestMapping("/addNoticeSubmit")
    @ResponseBody
    public HashMap<String,Object> noticeAdd( String topic, String content, HttpServletRequest req ){
        HashMap<String, Object> paramMap = new HashMap<>();
        HashMap<String, Object> resultMap = new HashMap<>();

        // 取出 Session 中存贮的值
        HttpSession session = req.getSession();
        Admin admin = (Admin) session.getAttribute(Constant.USER_SESSION);

        paramMap.put( "topic", topic );
        paramMap.put( "content", content );
        paramMap.put( "author", admin.getUsername() );
        paramMap.put( "createDate", new Date());

        // 执行添加公告的操作
        boolean flag = noticeService.noticeAdd(paramMap);
        if ( flag ){
            resultMap.put( "code", 0 );
        }

        return resultMap;
    }

    // 读者可以使用的 公告管理
    @RequestMapping("/noticeIndexOfReader")
    public String noticeIndexOfReaderJump(){
        return "notice/noticeIndexOfReader";
    }
}
